package com.zyc.studentstudy.utils

import android.text.TextUtils
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

/**
 * Created by zhangyuncai on 2021/1/23.
 * 说明：时间格式化工具类
 */
object TimeUtils {

    const val DAY = 24 * 60 * 60
    const val HOUR = 60 * 60
    const val MINUTE = 60

    val format_dd_MM_yyyy = SimpleDateFormat("dd-MM-yyyy", Locale.CHINA)
    val format_yyyy_MM_dd = SimpleDateFormat("yyyy_MM_dd", Locale.CHINA)
    const val text_yyyy_MM_dd = "yyyy-MM-dd"
    const val text_yyyy_MM_dd2 = "yyyyMMdd"
    const val text_yyyy_MM_dd3 = "yyyy/MM/dd"
    const val text_dd_MM_yyyy = "dd-MM-yyyy"
    const val text_dd_MM_yyyy2 = "dd/MM/yyyy"
    const val text_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss"
    const val text_yyyy_MM_dd_HH_mm_ss2 = "yyyyMMddHHmmss"
    const val text_yyyy_MM_dd_HH_mm_ss3 = "yyyy/MM/dd HH:mm:ss"
    const val text_yyyy_MM_dd_HH_mm_ss4 = "dd/MM/yyyy HH:mm:ss"
    const val text_yyyy_MM_dd_HH_mm_ss5 = "yyyy_MM_dd_HH_mm_ss"
    const val text_yyyy_MM_dd_HH_mm_ss6 = "yyyy-MM-dd HH:mm:ss"
    const val text_yyyy_MM_dd_HH_mm = "yyyy_MM_dd_HH_mm"
    const val text_yyyy_MM_dd_HH_mm2 = "yyyyMMdd HH:mm"
    const val text_yyyy_MM_dd_HH_mm3 = "yyyy/MM/dd HH:mm"
    const val text_yyyy_MM_dd_HH_mm4 = "dd/MM/yyyy HH:mm"
    const val text_hh_mm = "HH:mm"
    const val text_hh_mm_ss = "HHmmss"

    @JvmStatic
    fun parseTimeHHmmss(secondTime: Int): String {
        val hour = secondTime / (60 * 60)
        val minuter = secondTime % (60 * 60) / 60
        val second = secondTime % (60 * 60) % 60
        return String.format("%02d:%02d:%02d", hour, minuter, second)
    }

    /**
     * 获取开机时间
     *
     * @param time
     * @return
     */
    @JvmStatic
    fun parseOpenTime(time: String): String {
        val lTimes = time.toLong(16)
        val days = lTimes / DAY
        val hours = (lTimes / HOUR) % 24
        val minutes = (lTimes / MINUTE) % 60
        val seconds = lTimes % MINUTE
        return String.format(
            "%1\$dDay-%2\$dHour-%3\$dMinuter-%4\$dSecond",
            days,
            hours,
            minutes,
            seconds
        )
    }

    @JvmStatic
    fun createFileTimeName(): String {
        return format(Date().time, text_yyyy_MM_dd_HH_mm_ss5)
    }

    @JvmStatic
    fun getCurrentNumber(): Long {
        return Date().time
    }

    @JvmStatic
    fun getCurrentText(): String {
        return format(Date(), text_yyyy_MM_dd_HH_mm2)
    }

    @JvmStatic
    fun getCurrentYyyyMmDd(): String {
        return format(Date(), text_yyyy_MM_dd2)
    }

    @JvmStatic
    fun getCurrentHhMm(): String {
        return format(Date(), text_hh_mm)
    }

    /**
     * 转换日期格式
     * 将格式formatStr1转换为formatStr2
     *
     * @param text        例如:yyyy年MM月dd日
     * @param textFormat1 时间格式
     * @param textFormat2 时间格式
     * @return
     */
    @JvmStatic
    fun switchTextFormatter(text: String, textFormat1: String, textFormat2: String): String {
        if (TextUtils.isEmpty(text)) return ""
        val number = getNumber(text, textFormat1)
        return format(number, textFormat2)
    }

    /**
     * 时间转时间戳
     *
     * @param text       例如:yyyy年MM月dd日
     * @param textFormat 时间格式
     * @return 131 0000 0000 00  13位数的时间戳
     */
    @JvmStatic
    fun getNumber(text: String, textFormat: String): Long {
        val format = SimpleDateFormat(textFormat, Locale.CHINA)
        return try {
            format.parse(text).time
        } catch (e: Exception) {
            e.printStackTrace()
            0
        }
    }

    /**
     * 格式化时间
     *
     * @param numberText 131 0000 0000 00  13位数的时间戳
     * @param textFormat 时间格式
     * @return
     */
    @JvmStatic
    fun format(numberText: String, textFormat: String): String {
        return try {
            if (!TextUtils.isEmpty(numberText)) {
                val number = if (numberText.length == 10) {
                    (numberText + "000").toLong()
                } else {
                    numberText.toLong()
                }
                format(number, textFormat)
            } else {
                ""
            }
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    /**
     * 格式化时间
     *
     * @param number     131 0000 0000 00  13位数的时间戳
     * @param textFormat 时间格式
     * @return 返回对应的时间格式的字符串
     */
    @JvmStatic
    fun format(number: Long, textFormat: String): String {
        return try {
            format(Date(number), textFormat)
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    /**
     * 格式化时间
     *
     * @param date       new Date(131 0000 0000 00)
     * @param textFormat 时间格式 例如:yyyy年MM月dd日
     * @return 返回对应的时间格式的字符串
     */
    @JvmStatic
    fun format(date: Date, textFormat: String): String {
        val format = SimpleDateFormat(textFormat, Locale.CHINA)
        return format.format(date)
    }

    @JvmStatic
    fun getCurrentTimeValue(): Int {
        return (Date().time / 1000).toInt()
    }
}